﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FuncSortDelegate
{
	class Program
	{
		public enum SortOrder { ASC, DESC };
		class BubbleSortDouble
		{

			public static void Sort(double[] source, SortOrder sOrder)
			{
				double d;
				/*
				Func<double, double, bool> f = (x,  y) =>
				{
					if (x > y)
						return true;
					else
						return false;

				};
				*/
				// Делегат Func - только в краткой записи
				Func<double, double, bool> f1 = (x, y) => x > y;
				Func<double, double, bool> f2 = (x, y) => x < y;
				Func<double, double, bool> f;

				if (sOrder == SortOrder.ASC)
					f = f1;
				else
					f = f2;

				for (int i = 0; i < source.Length; i++)
				{
					for (int j = source.Length - 1; j > i; j--)
					{
						if (f(source[j - 1], source[j]))
						{
							d = source[j - 1];
							source[j - 1] = source[j];
							source[j] = d;
						}
					}
				}
			}
		}

		static void Main(string[] args)
		{
			int MaxLen = 10;
			double[] InputListToSort;
			double[] OutputListToSort;

			Console.Write("Input Number of elements: ");
			MaxLen = int.Parse(Console.ReadLine());

			InputListToSort = new double[MaxLen];
			OutputListToSort = new double[MaxLen];

			Console.WriteLine("\nRandom Source Array of double:");
			// Generate Randomize Array of Double
			Random fixRand = new Random();
			for (int i = 0; i < InputListToSort.Length; i++)
			{
				InputListToSort[i] = fixRand.NextDouble() * 100;
				OutputListToSort[i] = InputListToSort[i];
				Console.WriteLine(InputListToSort[i]);
			}

			// Сортируем по возрастанию
			BubbleSortDouble.Sort(OutputListToSort, SortOrder.ASC);

			// Print BubbleSort result
			Console.WriteLine("\nASC Bubble Sort Array :");
			foreach (double d in OutputListToSort)
			{
				Console.WriteLine(d);
			}

			// Сортируем по убыванию
			BubbleSortDouble.Sort(OutputListToSort, SortOrder.DESC);

			// Print BubbleSort result
			Console.WriteLine("\nDESC Bubble Sort Array:");
			foreach (double d in OutputListToSort)
			{
				Console.WriteLine(d);
			}

		}
	}
}
